﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using System.Web.SessionState;
using HKEA.CMS.Core.BLL.Tbl;
namespace WebApplication.ServerAshxFile
{
    /// <summary>
    /// DeleteBranchOffice 的摘要说明
    /// </summary>
    public class DeleteBranchOffice : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string str = string.Empty;
            context.Response.ContentType = "text/plain";
            string strImgID = context.Request.QueryString["UID"].ToString();
            string[] uid = strImgID.Split('-');

            tblCust_Branch cust_branchENT = new tblCust_Branch();
            tblCust_BranchBLL cust_branchBLL = new tblCust_BranchBLL();
            //if() 满足权限
            cust_branchENT.CustomerUID = Convert.ToInt32(uid[0]);
            cust_branchENT.CustBranchUID = Convert.ToInt32(uid[1]);
            cust_branchENT.Deleted = 0;
            cust_branchENT = cust_branchBLL.GetEntity(cust_branchENT);
            if (cust_branchENT != null)
            {
                cust_branchENT.Deleted = 1;
                cust_branchENT.LastModifyDate = DateTime.Now;
                cust_branchENT.LastModifyBy = GetManageName(context);
                if (cust_branchBLL.UpdateENT(cust_branchENT) > 0)
                {
                    str = "{\"Issucess\":\"1\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200011") + "\"}";
                    WriteToLoge(cust_branchENT.CustomerUID, cust_branchENT.BranchName_Eng);
                }
                else
                {
                    str = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200010") + "\"}";
                }
            }
            else
            {
                str = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200014") + "\"}";
            }
            context.Response.Write(str);
            context.Response.Flush();
        }
        public string GetManageName(HttpContext context)
        {
            string ret = string.Empty;
            if (context.Session[Constant.SessionName.Manage] != null)
            {
                ret = (context.Session[Constant.SessionName.Manage] as tblUser).User_Name;
            }
            else
            { ret = "system"; }
            return ret;
        }
        protected void WriteToLoge(int customerUID, string BranchName)
        {
            tblHistory entlog = new tblHistory();
            BasePage page = new BasePage();
            tblCustomer Company = tblCustomerBLL.GetInstance().GetEntity(customerUID);
            tblUser ent = page.GetManageFromSession();
            entlog.HistoryCatUID = 2;
            entlog.HistoryTypeUID = 6;
            entlog.CustomerUID = customerUID;
            entlog.Cust_MemberUID = 0;
            entlog.ContactPersonUID = 0;
            entlog.History_Detail = ent.User_Name + " {" + Company.Name_Eng + "}{Branch Office}Delete " + BranchName;
            entlog.LastModifyBy = ent.User_Name;
            int tal = HKEA.CMS.Core.BLL.Tbl.tblHistoryBLL.GetInstance().InsertENT(entlog);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}